package me.lxw.blogsystem.servlet.admin;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import me.lxw.blogsystem.dao.ArticleDAO;

@WebServlet("/admin/articleDel.do")
public class ArticleDelServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
        IOException {
        // 获取分页参数
        String idStr = request.getParameter("id");
        int id = -1;
        try {
            id = Integer.valueOf(idStr);
        } catch (NumberFormatException e1) {
            e1.printStackTrace();
        }
        if (id > -1) {
            // 实例化 ArticleDAO
            ArticleDAO articleDAO = new ArticleDAO();
            // 获取数据
            try {
                // 逻辑删除操作
                articleDAO.del(id);
                response.sendRedirect(request.getContextPath() + "/admin/articleList.do?page=1");
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
